package com.awspaas.user.apps.cpq.event;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import com.actionsoft.apps.listener.AppListener;
import com.actionsoft.apps.resource.AppContext;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.awspaas.user.apps.cpq.util.RefreshDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshModulDataUtils;
import com.awspaas.user.apps.cpq.util.RefreshTempDataUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

public class GetRabbitMsgAppAfterStart implements AppListener{
	private final static String QUEUE_NAME = "test_queue";
	@Override
	public void after(AppContext arg0) {
		
		
	}

	@Override
	public boolean before(AppContext pec) {
		try {
			
			Thread thread=new Thread(new Runnable() {
				
				@Override
				public void run() {
					
					try {
						getCo();
					} catch (IOException | TimeoutException | InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			
			thread.start();
			return true;
		} catch (Exception e2) {
			throw e2;
		}
		   
		
		
	}
private void getCo() throws IOException, TimeoutException, InterruptedException {
	ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
 
    try {
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        channel.basicQos(1);
        System.out.println(" [*] Waiting for messages...");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
          String bindId=""; 
        	
            try {
                String message = new String(delivery.getBody(), "UTF-8");
                if(message!=null&&!message.isEmpty()) {
                	 System.out.println(" [x] Received '" + message + "'");                
                	
                }
               
                // 确保通道未关闭再确认
                if(channel.isOpen()) {
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                    System.out.println(" [√] Message processed");
                }
               
            } catch (Exception e) {
                System.err.println(" [×] Process failed: " + e.getMessage());
                System.err.println(bindId);
                String sql="update BO_EU_CPQ_COMPONENT_PART set TS='可编辑' where BINDID=? ";
                int num=DBSql.update(sql,new Object[] {bindId});
                 num=SDK.getBOAPI().updateByBindId("BO_EU_CPQ_COMPONENT_PART", bindId,"TS", "可编辑");
                System.err.println(num);
         	   if(channel.isOpen()) {
                	  channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                }
               // throw e;
            }
        };
      

        channel.basicConsume(QUEUE_NAME, false, deliverCallback, consumerTag -> {});
        // 保持主线程运行
        while(true) {
            Thread.sleep(1000);
        }
    } finally {
        if(channel != null && channel.isOpen()) channel.close();
        if(connection != null && connection.isOpen()) connection.close();
     
    }
}
}
